const http = require("http");
const url = require("url");
const fs = require("fs");
const https = require("https");

const server = http.createServer();

server.on("listening",()=>{
    console.log("启动服务器")
})

server.on("request",(request,response)=>{
    //解析请求request
    const objurl = url.parse(request.url)
    if(objurl.pathname == "/"){
        fs.readFile("./index.html",(err,data)=>{
            response.write(data);
            response.end();
        })
    }else if(objurl.pathname.startsWith("/v4")){
        //创建请求转发
        const req = https.request({
            hostname:"m.maizuo.com",
            port:443,
            path:objurl.path,
            method:"GET"
        },(res)=>{
            let result = "";
            res.on("data",(data)=>{
                result += data;
            })
            res.on("end",()=>{
                response.write(result);
                response.end()
            })
        })
        //发送请求 
        req.end()
    }
})

server.listen("8080","localhost")